<?php

class Router {
    public static $routes = array();
    public static $cache = array(); // routes are mapped only onces and cached here.

    public static function route() {
        $url = isset($_REQUEST['__mini']) ? $_REQUEST['__mini'] : '/';

        if (strlen($url) > 0 && $url[0] !== '/') {
            $url = '/'.$url;
        }

        if ($url === '/index.php') { // redirect user to clean url
            header("Location: /");
            return;
        }

        // prevent ../../ file access attempt
        $url = preg_replace('/\.\.\//', '', $url);

        $response = new Response(new Request($url));
        $response->process();
        $response->send();
    }

    public static function connect($route, $default = array(), $tokens = array()) {

        if (empty($default['controller'])) {
            $default['controller'] = '';
        }

        if (empty($default['action'])) {
            $default['action'] = 'index';
        }

        // default application route
        if ($route == '' || $route == '/') {
            self::$routes[] = array('route' => '/', 'pattern' => '/^\/$/', 'default' => $default, 'tokens' => $tokens);
            return;
        }

        // customized application route
        $pattern = $route;
        foreach ($tokens as $token => $tokenRegex) {
            $pattern = preg_replace("/$token/", $tokenRegex, $pattern);
        }

        $pattern = preg_replace("/\//", "\/", $pattern);

        self::$routes[] = array('route' => $route, 'pattern' => "/^{$pattern}$/", 'default' => $default, 'tokens' => $tokens);
    }

    public static function to_params($url) {
        $params = array();

        //Logger::debug($url);
        //Logger::debug(print_r(self::$routes, true));

        foreach (self::$routes as $i => $route) {
            if (preg_match_all($route['pattern'], $url, $matches) != 1) {
                continue;
            }

            // matched the special route
            //Logger::debug("matched route: {$route['route']}");
            //Logger::debug(print_r($route, true));
            //Logger::debug(print_r($matches, true));

            // copy over the default params
            $params = $route['default'];

            // inject the token params
            $tokens = $route['tokens'];
            $index = 1;
            foreach ($tokens as $token => $tokenRegex) {
                $params[substr($token, 1)] = $matches[$index][0];
                $index++;
            }
        }

        if (!empty($params)) {
            return $params;
        }

        // fall back to the default routing mechanism
        $tokens = explode('/', substr($url, 1)); //Logger::debug(print_r($tokens, true));

        $params = array();

        foreach ($tokens as $i => $token) {
            if ($i == 0) {
                $params['controller'] = $token;
            }

            if ($i == 1) {
                $params['action'] = $token;
            }

            if ($i > 1) {
                $params[':'.($i-1)] = $token;
            }
        }

        return $params;
    }

    public static function to_url($params) {

    }
}